/**
 * 
 */
package info.niwota.ziplock;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;

/**
 * @author qiangli
 */
class Eula {
	private static final CharSequence EULA_TITLE = "End User License Agreement";
	
	static void show(final Activity ctx) {
		final SharedPreferences pref = ctx.getSharedPreferences(
				Preferences.EULA, Activity.MODE_PRIVATE);
		if (!pref.getBoolean(Preferences.EULA_ACCEPTED, false)) {
			final AlertDialog.Builder builder = new AlertDialog.Builder(
					ctx);
			builder.setTitle(EULA_TITLE);
			builder.setCancelable(true);
			builder.setPositiveButton(R.string.action_accept,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							accept(pref);
						}
					});
			builder.setNegativeButton(R.string.action_reject,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							reject(ctx);
						}
					});
			builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
				public void onCancel(DialogInterface dialog) {
					reject(ctx);
				}
			});

			builder.setMessage(Utils.readResource(ctx, R.raw.eula));
			builder.create().show();
		}
	}

	private static void accept(SharedPreferences preferences) {
		preferences.edit().putBoolean(Preferences.EULA_ACCEPTED, true).commit();
	}

	private static void reject(Activity ctx) {
		Notifications.removeNotification(ctx);
		ctx.finish();
	}
}
